package com.mucas;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;

public class Connector {
	private URL source;
	
	public Connector() {
		this.source = null;
	}
	
	public boolean setURL(String url) throws Exception {
		try {
			this.source = new URL(url);
			return true;
		} catch (MalformedURLException e) {
			return false;
		}
	}
	
    public InputStream OpenHttpConnection() throws IOException {
    	InputStream in     = null;
		int response   	   = -1;
		URLConnection conn = this.source.openConnection();
		
		if (!(conn instanceof HttpURLConnection))  
			return null;
		
		try {
			HttpURLConnection httpConn = (HttpURLConnection) conn;
	        httpConn.setAllowUserInteraction(false);
	        httpConn.setInstanceFollowRedirects(true);
	        httpConn.setRequestMethod("GET");
	        httpConn.connect(); 
	
	        response = httpConn.getResponseCode();    
	        
	        if (response == HttpURLConnection.HTTP_OK) {
	            in = httpConn.getInputStream();     
	        }  
		} catch(Exception ex) {
			return null;
		}
		return in;
    }
}
